# Create your views here.

from django.views import View

from model.models import User
from server.utils import Response, Request, LoginRequiredSuperAdmin, Verify, Code


class ApplyAdminView(LoginRequiredSuperAdmin, View):
    """ Application administrator """

    @Request.getJson
    def post(self, request):
        """ Approval Application Administrator """
        uid = request.JSON.get('uid')
        status = request.JSON.get('status')

        code = Verify.userUid(uid)
        if code != Code.OK:
            return Response.Json(code=code)
        if int(status) not in [User.Status.apply_approve, User.Status.apply_not_approve]:
            return Response.Json(code=Code.BadRequest)

        user = User.objects.get(uid=uid)
        if user.role != User.Role.person:
            return Response.Json(code=Code.Error, msg='The user is already an administrator')
        elif user.status != User.Status.applying:
            return Response.Json(code=Code.Error, msg='The user has not applied for an administrator')

        user.set_status(status)
        return Response.Json()
